<?php
namespace Sys\Controller;
use Todo\Controller\ApiController;
use Todo\Util\ArrayUtil;

class MenuController extends ApiController {

    public function tree(){
        $this->_field = 'id,pid,title text,url,type';
        $this->_loadModel();
        $where = $this->_search();
        $this->_filter($where);
        $list = $this->_model->field($this->_field)->pageSelect($where, 0, 1000, $this->_order);
        $data = ArrayUtil::getTree($list, 0);
        $this->apiSuccess($data);
    }

    /**
     * 按应用展示全部菜单 ，如果有角色信息，则选中
     */
    public function app(){
        $roleId = I('get.role_id');
        if($roleId) $menuList = D('Todo/Sys/RoleMenu')->where(array('role_id'=>$roleId))->getField('menu_id', true);

        #按应用进行菜单分组
        $appList = D('Todo/Sys/Application')->field('id,title text')->where(array('status'=>1))->select();
        foreach ($appList as &$app){
            $list = D('Todo/Sys/Menu')->where(array('app_id'=>$app['id']))->field('id,pid,title text')->select();
            if($roleId) {
                foreach ($list as &$menu){
                    if(in_array($menu['id'], $menuList)) $menu['checked'] = true;
                }
            }
            $app['id'] = 'app_'.$app['id'];
            $app['children'] = ArrayUtil::getTree($list);
        }
        $this->apiSuccess($appList);
    }
    
}